local L = LibStub("AceLocale-3.0"):GetLocale("TipTop")
local f = CreateFrame("FRAME")
local LSM = LibStub("LibSharedMedia-3.0")
local widgetLists = AceGUIWidgetLSMlists

local positions = {"TOP", "TOPRIGHT", "TOPLEFT",
					"BOTTOM", "BOTTOMRIGHT", "BOTTOMLEFT",
					"RIGHT", "LEFT", "CENTER"}

local defaults = {
	profile = {
		alpha = 1,
		scale = 1,
		font = "Friz Quadrata TT",
		bg = "brushed",
		bgColor = {r=.09, g=.09, b=.19},	--TOOLTIP_DEFAULT_BACKGROUND_COLOR
		border = "Seerah Solid",
		borderWidth = 2.5,
		classColor = true,
		itemColor = true,
		borderColor = {r=.6, g=.6, b=.6, a=1},
		healthBar = "TipTop Default",
		sbarbg = "TipTop Default",
		sbarbgcolor = {r=0, g=0, b=0, a=.4},
		topBar = false,
		raidIconSize = 16,
		raidIconPosition = "TOP",
		raidIconX = 0,
		raidIconY = 0,
		onCursor = false,
		anchor = "BOTTOMRIGHT",
		offsetX = -2,
		offsetY = 109,
		showTargetText = true,
		you = true,
		showTalentText = true,
		healthText = true,
	},
}

local options = {
	name = L["TipTop Options"],
	type = "group",
	args = {
		header1 = {
			name = L["Appearance"],
			type = "header",
			order = 1,
		},
		font = {
			name = L["Font"],
			desc = L["Select the font for the tooltip and health text."],
			type = "select",
			dialogControl = "LSM30_Font",
			values = widgetLists.font,
			get = function()
					return db.font
--					for k, v in pairs(widgetLists.font) do
--						if v == db.font then
--							return k
--						end
--					end
				end,
			set = function(self, font)
					db.font = font
--			set = function(_, font)
--					db.font = widgetLists.font[font]
					TipTop:SetUpTips()
				end,
			order = 2,
		},
		scale = {
			name = L["Tooltip scale"],
			desc = L["The scale of the entire tooltip."],
			type = "range",
			min = .5,
			max = 2,
			step = .05,
			get = function() return db.scale end,
			set = function(_, scale) 
						db.scale = scale
						TipTop:SetUpTips()
					end,
			order = 3,
		},
		bgColor = {
			name = L["Background Color/Alpha"],
			desc = L["The background color and transparency.\n\nIf you wish to use a custom background texture and do not want it colored, set this to white."],
			type = "color",
			hasAlpha = true,
			get = function() return db.bgColor.r, db.bgColor.g, db.bgColor.b, db.alpha end,
			set = function(_,r,g,b,a)
					db.bgColor.r = r
					db.bgColor.g = g
					db.bgColor.b = b
					db.alpha = a
					TipTop:SetUpTips()
				end,
			order = 4,
		},
		bg = {
			name = L["Background"],
			desc = L["Texture to use as the tip's background."],
			type = "select",
			dialogControl = "LSM30_Background",
			values = widgetLists.background,
			get = function()
					return db.bg
				end,
			set = function(self, bg)
					db.bg = bg
					TipTop:SetUpTips()
				end,
			order = 5,
		},
		empty1 = {
			name = "   ",
			type = "description",
			order = 6,
		},
		header2 = {
			name = L["Health Bar"],
			type = "header",
			order = 7,
		},
		healthBar = {
			name = L["Health bar texture"],
			desc = L["Texture to use for the status bar below the tooltip."],
			type = "select",
			dialogControl = "LSM30_Statusbar",
			values = widgetLists.statusbar,
			get = function()
					return db.healthBar
				end,
			set = function(self, healthBar)
					db.healthBar = healthBar
					TipTop:SetUpTips()
				end,
			order = 8,
		},
		healthText = {
			name = L["Health text"],
			desc = L["If checked, health percentage will be displayed on the tooltip's status bar."],
			type = "toggle",
			get = function() return db.healthText end,
			set = function()
						db.healthText = not db.healthText
						if db.healthText == true then
							ttHealth:Show()
						else
							ttHealth:Hide()
						end
					end,
			order = 8.5,
		},
		sbarbg = {
			name = L["Background texture"],
			desc = L["Texture to use for the status bar's background."],
			type = "select",
			dialogControl = "LSM30_Statusbar",
			values = widgetLists.statusbar,
			get = function()
					return db.sbarbg
				end,
			set = function(self, sbarbg)
					db.sbarbg = sbarbg
					TipTop:SetUpTips()
				end,
			order = 8.75,
		},
		sbarbgcolor = {
			name = L["Background Color"],
			desc = L["The color of the health bar's background."],
			type = "color",
			hasAlpha = true,
			get = function() return db.sbarbgcolor.r, db.sbarbgcolor.g, db.sbarbgcolor.b, db.sbarbgcolor.a end,
			set = function(_,r,g,b,a)
					db.sbarbgcolor.r = r
					db.sbarbgcolor.g = g
					db.sbarbgcolor.b = b
					db.sbarbgcolor.a = a
					TipTop:SetUpTips()
				end,
			order = 9,
		},
		topBar = {
			name = L["Health bar on top"],
			desc = L["If checked, the health bar will be displayed above the tooltip instead of below."],
			type = "toggle",
			get = function() return db.topBar end,
			set = function()
						db.topBar = not db.topBar
						TipTop:SetUpTips()
					end,
			order = 9.5,
		},
		empty2 = {
			name = "   ",
			type = "description",
			order = 10,
		},
		header3 = {
			name = L["Borders"],
			type = "header",
			order = 11,
		},
		border = {
			name = L["Border"],
			desc = L["Texture for the tooltip borders."],
			type = "select",
			dialogControl = "LSM30_Border",
			values = widgetLists.border,
			get = function()
					return db.border
				end,
			set = function(self, border)
					db.border = border
					TipTop:SetUpTips()
				end,
			order = 12,
		},
		borderWidth = {
			name = L["Border Width"],
			desc = L["Width of the border"],
			type = "range",
			min = 1,
			max = 16,
			step = .5,
			get = function() return db.borderWidth end,
			set = function(_, borderWidth) 
						db.borderWidth = borderWidth
						TipTop:SetUpTips()
					end,
			order = 13,
		},
		borderColor = {
			name = L["Border color"],
			desc = L["Color of the border when not class or item quality colored"],
			type = "color",
			hasAlpha = true,
			get = function() return db.borderColor.r, db.borderColor.g, db.borderColor.b, db.borderColor.a end,
			set = function(_,r,g,b,a)
						db.borderColor.r = r
						db.borderColor.g = g
						db.borderColor.b = b
						db.borderColor.a = a
						TipTop:SetUpTips()
					end,
			order = 14,
		},
		classColor = {
			name = L["Class-colored borders"],
			desc = L["If checked, the border is colored by class when mousing over a unit"],
			type = "toggle",
			get = function() return db.classColor end,
			set = function()
						db.classColor = not db.classColor
					end,
			order = 15,
		},
		itemColor = {
			name = L["Item quality borders"],
			desc = L["If checked, the border is colored by item quality when mousing over an item"],
			type = "toggle",
			get = function() return db.itemColor end,
			set = function()
						db.itemColor = not db.itemColor
					end,
			order = 16,
		},
		empty3 = {
			name = "   ",
			type = "description",
			order = 17,
		},
		header4 = {
			name = L["Raid Icon"],
			type = "header",
			order = 18,
		},
		raidIconSize = {
			name = L["Raid icon size"],
			desc = L["Width and height of the raid icon on the tooltip"],
			type = "range",
			min = 8,
			max = 32,
			step = 1,
			get = function() return db.raidIconSize end,
			set = function(_, raidIconSize) 
						db.raidIconSize = raidIconSize
						TipTop:SetUpTips()
					end,
			order = 19,
		},
		raidIconPosition = {
			name = L["Raid icon position"],
			desc = L["What side of the tip you would like the icon located at."],
			type = "select",
			values = positions,
			get = function()
						for k, v in pairs(positions) do
							if db.raidIconPosition == v then
								return k
							end
						end
					end,
			set = function(_, raidIconPosition)
						db.raidIconPosition = positions[raidIconPosition]
						TipTop:SetUpTips()
					end,
			order = 20,
		},
		raidIconX = {
			name = L["X Offset"],
			desc = L["Offsets from the icon's position left/right"],
			type = "input",
			get = function() return db.raidIconX end,
			set = function(_,raidIconX)
						db.raidIconX = raidIconX
						TipTop:SetUpTips()
					end,
			order = 21,
		},
		raidIconY = {
			name = L["Y Offset"],
			desc = L["Offsets from the icon's position up/down"],
			type = "input",
			get = function() return db.raidIconY end,
			set = function(_,raidIconY)
						db.raidIconY = raidIconY
						TipTop:SetUpTips()
					end,
			order = 22,
		},
		empty4 = {
			name = "   ",
			type = "description",
			order = 23,
		},
		header5 = {
			name = L["Position"],
			type = "header",
			order = 24,
		},
		onCursor = {
			name = L["On Cursor"],
			desc = L["Whether you want the tooltip attached to the cursor or not.\n\nNote: If checked, anchor and offsets below do not apply."],
			type = "toggle",
			get = function() return db.onCursor end,
			set = function()
						db.onCursor = not db.onCursor
					end,
			order = 25,
		},
		anchor = {
			name = L["Tip Anchor"],
			desc = L["What side of the screen you would like the tooltip located at."],
			type = "select",
			disabled = function() return db.onCursor end,
			values = positions,
			get = function()
						for k, v in pairs(positions) do
							if db.anchor == v then
								return k
							end
						end
					end,
			set = function(_, anchor)
						db.anchor = positions[anchor]
					end,
			order = 26,
		},
		offsetX = {
			name = L["X Offset"],
			desc = L["Offsets from the tooltip's anchor left/right"],
			type = "input",
			disabled = function() return db.onCursor end,
			get = function() return db.offsetX end,
			set = function(_,offsetX)
						db.offsetX = offsetX
					end,
			order = 27,
		},
		offsetY = {
			name = L["Y Offset"],
			desc = L["Offsets from the tooltip's anchor up/down"],
			type = "input",
			disabled = function() return db.onCursor end,
			get = function() return db.offsetY end,
			set = function(_,offsetY)
						db.offsetY = offsetY
					end,
			order = 28,
		},
		empty5 = {
			name = "   ",
			type = "description",
			order = 29,
		},
		header6 = {
			name = L["Extra Lines"],
			type = "header",
			order = 30,
		},
		showTargetText = {
			name = L["Target text"],
			desc = L["Whether you wish to view the mouseover unit's target in the tooltip"],
			type = "toggle",
			get = function() return db.showTargetText end,
			set = function()
						db.showTargetText = not db.showTargetText
					end,
			order = 31,
		},
		you = {
			name = L["Target emphasis"],
			desc = L["When checked, if the mouseover unit's target is you, it will be displayed as <<YOU>>."],
			type = "toggle",
			disabled = function() return not db.showTargetText end,
			get = function() return db.you end,
			set = function()
						db.you = not db.you
					end,
			order = 32,
		},
		showTalentText = {
			name = L["Talent text"],
			desc = L["Whether you wish to view the mouseover unit's talent info in the tooltip"],
			type = "toggle",
			get = function() return db.showTalentText end,
			set = function()
						db.showTalentText = not db.showTalentText
					end,
			order = 33,
		},
	},
}

local function OnInitialize()
	TipTop.db = LibStub("AceDB-3.0"):New("TipTopDB", defaults, "Default")
	db = TipTop.db.profile
	LibStub("AceConfig-3.0"):RegisterOptionsTable("TipTop", options)
	TipTop.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("TipTop", "TipTop")
	
	LSM:Register("statusbar", "TipTop Default", "Interface\\AddOns\\TipTop\\bar2.blp")
	LSM:Register("statusbar", "TipTop Alt", "Interface\\AddOns\\TipTop\\bar1.blp")
	LSM:Register("border", "Seerah Solid", "Interface\\AddOns\\TipTop\\SeerahSolidBorder.blp")
	LSM:Register("background", "brushed", "Interface\\AddOns\\TipTop\\brushed.tga")
end

	
f:SetScript("OnEvent", OnInitialize)
f:RegisterEvent("PLAYER_LOGIN")